package com.atguigu.gulimall.search.thread;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @ ClassName SpinLock
 * @ Description TODO
 * @ Author admin
 * @ Date 2021/3/17 10:10
 * @ Version 1.0
 */
public class SpinLock {

    private AtomicReference<Thread> owner = new AtomicReference<>();

    public void lock() throws InterruptedException {
        Thread current = Thread.currentThread();
        while (!owner.compareAndSet(null,current)){
//            Thread.sleep(100);
//            System.out.println(current.getId()+" 锁正在自旋");
        }
    }

    public void unlock(){
        Thread current = Thread.currentThread();
        owner.compareAndSet(current,null);
    }


}
